﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace SQLServerHealthCheck.Model
{
    public partial class Issue
    {

        HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString());

        public Server Server
        {
            get
            {
                return (Server)GetObject(1);
            }
        }

        public Service Service
        {
            get { return (Service)GetObject(2); }

        }
        public Database Database
        {
            get { return (Database)GetObject(3); }

        }
        public Table Table
        {
            get { return (Table)GetObject(5); }

        }
        public Index Index
        {
            get { return (Index)GetObject(6); }

        }
        public DatabaseFile DatabaseFile
        {
            get { return (DatabaseFile)GetObject(4); }

        }
        private Object GetObject(int scopeId)
        {
            int? targetId = this.TargetId;
            if (targetId.HasValue)
            {
                //Server
                if (scopeId == this.IssueType.ScopeId && this.IssueType.ScopeId == 1)
                {
                    return dc.Servers.FirstOrDefault(s => s.ServerID == targetId.Value);
                }
                //Service
                else if (scopeId <= this.IssueType.ScopeId && this.IssueType.ScopeId ==2)
                {
                    if (scopeId == 1)
                    {
                        return dc.Services.FirstOrDefault(s => s.ServiceID == targetId.Value).Server;
                    }
                    else
                    {
                        return dc.Services.FirstOrDefault(s => s.ServiceID == targetId.Value);
                    }

                }
                //Database
                else if (scopeId <= this.IssueType.ScopeId && this.IssueType.ScopeId == 3)
                {

                    if (scopeId == 1)
                    {
                        return dc.Databases.FirstOrDefault(d => d.DatabaseID == targetId.Value).Service.Server;
                    }
                    else if (scopeId == 2)
                    {
                        return dc.Databases.FirstOrDefault(d => d.DatabaseID == targetId.Value).Service;
                    }
                    else
                    {
                        return dc.Databases.FirstOrDefault(d => d.DatabaseID == targetId.Value);
                    }
                }
                //File
                else if (scopeId <= this.IssueType.ScopeId && this.IssueType.ScopeId == 4)
                {
                    if (scopeId == 1)
                    {
                        return dc.DatabaseFiles.FirstOrDefault(f => f.DatabaseFileID == targetId.Value).Database.Service.Server;
                    }
                    else if (scopeId == 2)
                    {
                        return dc.DatabaseFiles.FirstOrDefault(f => f.DatabaseFileID == targetId.Value).Database.Service;
                    }
                    else if (scopeId == 3)
                    {
                        return dc.DatabaseFiles.FirstOrDefault(f => f.DatabaseFileID == targetId.Value).Database;
                    }
                    else
                    {
                        return dc.DatabaseFiles.FirstOrDefault(f => f.DatabaseFileID == targetId.Value);

                    }
                }
                //Table
                else if (scopeId <= this.IssueType.ScopeId && this.IssueType.ScopeId == 5)
                {
                    if (scopeId == 1)
                    {
                        return dc.Tables.FirstOrDefault(t => t.TableID == targetId.Value).Database.Service.Server;
                    }
                    else if (scopeId == 2)
                    {
                        return dc.Tables.FirstOrDefault(t => t.TableID == targetId.Value).Database.Service;
                    }
                    else if (scopeId == 3)
                    {
                        return dc.Tables.FirstOrDefault(t => t.TableID == targetId.Value).Database;
                    }
                    else if (scopeId == 4)
                    {
                        return dc.Tables.FirstOrDefault(t => t.TableID == targetId.Value).Database.DatabaseFiles;
                    }
                    else
                    {
                        return dc.Tables.FirstOrDefault(t => t.TableID == targetId.Value);
                    }

                }
                //Index
                else if (scopeId <= this.IssueType.ScopeId && this.IssueType.ScopeId == 6)
                {
                    if (scopeId == 1)
                    {
                        return dc.Indexes.FirstOrDefault(i => i.IndexID == targetId.Value).Table.Database.Service.Server;
                    }
                    else if (scopeId == 2)
                    {
                        return dc.Indexes.FirstOrDefault(i => i.IndexID == targetId.Value).Table.Database.Service;
                    }
                    else if (scopeId == 3)
                    {
                        return dc.Indexes.FirstOrDefault(i => i.IndexID == targetId.Value).Table.Database;
                    }
                    else if (scopeId == 4)
                    {
                        return dc.Indexes.FirstOrDefault(i => i.IndexID == targetId.Value).Table.Database.DatabaseFiles;
                    }
                    else if (scopeId == 5)
                    {
                        return dc.Indexes.FirstOrDefault(i => i.IndexID == targetId.Value).Table;
                    }
                    else
                    {
                        return dc.Indexes.FirstOrDefault(i => i.IndexID == targetId.Value);
                    }

                }


            }


            return null;

        }

    }
}
